﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.Model;
using SalePartial.BLL;
using Telerik.Web.UI;
using XunPan.Web.Globals;
using SalePartial.Utility;
using System.Web.Security;
using System.Text;
using System.Data;

public partial class Login_regSupplies : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if (User.Identity.IsAuthenticated)
            //{
            //    Response.Clear();
            //    Response.Redirect(GlobalSetting.UrlHomePage, true);

            //}

            showPageFromSession();
            Master.FinshOneProcess(REG_PROCESS.SUPPY_INFO, REG_STATUS.USED);
        }
    }

    protected void btnUploadPhoto_Click(object sender, System.EventArgs e)
    {
        if (ruploadPhoto.UploadedFiles.Count > 0)
        {


            string oldfilename = ruploadPhoto.UploadedFiles[0].GetName();

            string newfilename = null;
            string oldpath = null;
            string newpath = null;

            oldpath = Server.MapPath(ruploadPhoto.TargetFolder) + "\\" + oldfilename;


            newfilename = FileUtility.CreateUniqueFileName(oldfilename);
            newpath = Server.MapPath(ruploadPhoto.TargetFolder) + "\\" + newfilename;

            System.IO.File.Move(oldpath, newpath);



            imgPhoto.ImageUrl = ruploadPhoto.TargetFolder + "/" + newfilename;

            btnDeletePhoto.Visible = btnUploadPhoto.Visible;
            btnUploadPhoto.Visible = !btnDeletePhoto.Visible;
            ruploadPhoto.Visible = !btnDeletePhoto.Visible;

        }
    }

    protected void btnDeletePhoto_Click(object sender, System.EventArgs e)
    {
        string filepath = Server.MapPath(imgPhoto.ImageUrl);
        if (!System.IO.Path.GetFileNameWithoutExtension(filepath).Equals("blank_photo"))
        {
            try
            {
                System.IO.File.Delete(filepath);
            }
            catch (Exception ex)
            {
                return;

            }

            imgPhoto.ImageUrl = "~/images/logon/blank_photo.jpeg";

            btnDeletePhoto.Visible = btnUploadPhoto.Visible;
            btnUploadPhoto.Visible = !btnDeletePhoto.Visible;
            ruploadPhoto.Visible = !btnDeletePhoto.Visible;
        }

    }
    protected void btnUploadLogo_Click(object sender, System.EventArgs e)
    {

        if (ruploadLogo.UploadedFiles.Count > 0)
        {
            string oldfilename = ruploadLogo.UploadedFiles[0].GetName();
            string newfilename = null;
            string oldpath = null;
            string newpath = null;

            oldpath = Server.MapPath(ruploadLogo.TargetFolder) + "\\" + oldfilename;

            newfilename = FileUtility.CreateUniqueFileName(oldfilename);
            newpath = Server.MapPath(ruploadLogo.TargetFolder) + "\\" + newfilename;

            System.IO.File.Move(oldpath, newpath);



            imgLogo.ImageUrl = ruploadLogo.TargetFolder + "/" + newfilename;


            btnDeleteLogo.Visible = btnUploadLogo.Visible;
            btnUploadLogo.Visible = !btnDeleteLogo.Visible;
            ruploadLogo.Visible = !btnDeleteLogo.Visible;
        }

    }
    protected void btnDeleteLogo_Click(object sender, System.EventArgs e)
    {
        string filepath = Server.MapPath(imgLogo.ImageUrl);
        if (!System.IO.Path.GetFileNameWithoutExtension(filepath).Equals("blank_logo"))
        {
            try
            {
                System.IO.File.Delete(filepath);
            }
            catch (Exception ex)
            {
                return;

            }

            imgLogo.ImageUrl = "~/images/logon/blank_logo.jpeg";

            btnDeleteLogo.Visible = btnUploadLogo.Visible;
            btnUploadLogo.Visible = !btnDeleteLogo.Visible;
            ruploadLogo.Visible = !btnDeleteLogo.Visible;
        }
    }

    protected void btnUploadLicense_Click(object sender, System.EventArgs e)
    {
        if (ruploadLicense.UploadedFiles.Count > 0)
        {
            string oldfilename = ruploadLicense.UploadedFiles[0].GetName();
            string newfilename = null;
            string oldpath = null;
            string newpath = null;
            Trace.Warn(oldfilename);


            oldpath = Server.MapPath(ruploadLicense.TargetFolder) + "\\" + oldfilename;


            newfilename = FileUtility.CreateUniqueFileName(oldfilename);
            newpath = Server.MapPath(ruploadLicense.TargetFolder) + "\\" + newfilename;

            System.IO.File.Move(oldpath, newpath);

            lnkLicense.Text = oldfilename;
            lnkLicense.NavigateUrl = ruploadLicense.TargetFolder + "/" + newfilename;

            //将License文件的大小和类型信息存入btn中
            btnUploadLicense.CommandName = ruploadLicense.UploadedFiles[0].ContentType;
            btnUploadLicense.CommandArgument = ruploadLicense.UploadedFiles[0].ContentLength.ToString();


            lnkLicense.Visible = btnUploadLicense.Visible;
            btnDeleteLicense.Visible = btnUploadLicense.Visible;
            btnUploadLicense.Visible = !btnDeleteLicense.Visible;
            ruploadLicense.Visible = !btnDeleteLicense.Visible;
        }


    }

    protected void btnDeleteLicense_Click(object sender, System.EventArgs e)
    {
        string filepath = Server.MapPath(lnkLicense.NavigateUrl);

        System.IO.File.Delete(filepath);

        lnkLicense.Visible = btnUploadLicense.Visible;
        btnDeleteLicense.Visible = btnUploadLicense.Visible;
        btnUploadLicense.Visible = !btnDeleteLicense.Visible;
        ruploadLicense.Visible = !btnDeleteLicense.Visible;

    }

    protected void ibtBack_Click(object sender, System.EventArgs e)
    {
        Master.GoBackPreProcess();

        //Page.Response.Redirect("~/Login/RegPerson.aspx", true);
        string prePage = (string)Session[ConstantSession.PRE_PAGE_ID];
        if (prePage != null)
        {
            Page.Response.Redirect(prePage, true);
        }
    }

    protected void ibtPurchase_Click(object sender, System.EventArgs e)
    {
        if (!savePage())
        {
            return;
        }

        Page.Response.Redirect("~/Login/RegPurchases.aspx", true);
    }

    protected void ibtSubmit_Click(object sender, System.EventArgs e)
    {

        if (!savePage())
        {
            return;
        }

        Page.Response.Redirect("~/Login/regConfirm.aspx", true);
    }

    private bool savePage()
    {
        List<XPCompanyCertInfo> certInfoList =
   (List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST];

        if (certInfoList == null || certInfoList.Count == 0)
        {
            //rgridCompanyCerts.Controls.Add(new LiteralControl("您还未添加任何资质认证哦..."));
            return false;
        }

        List<XPCompanyProductInfo> prdctInfoList =
            (List<XPCompanyProductInfo>)Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST];

        if (prdctInfoList == null || prdctInfoList.Count == 0)
        {
            //rgridCompanyProducts.Controls.Add(new LiteralControl("您还未添加贵公司的产品哦..."));
            return false;
        }

        RadCaptchaReg.Validate();
        if (!RadCaptchaReg.IsValid)
        {
            return false;
        }
        saveToSession();
        return true;
    }

    private Boolean showPageFromSession()
    {
        XPCompanyInfo newcompany = (XPCompanyInfo)Session[ConstantSession.REG_SUPPLY_COMPY_INFO];

        if (newcompany == null)
        {
            newcompany = (XPCompanyInfo)Session[ConstantSession.REG_PURCH_COMPY_INFO];
        }
        if (newcompany != null)
        {
            tbCompanyName.Text = newcompany.CompanyName;

            foreach (RadComboBoxItem item in rcbIndustry.Items)
            {
                if (item.Text == newcompany.Industry)
                {
                    rcbIndustry.SelectedIndex = item.Index;
                    break;
                }
            }
            //rcbIndustry.Text = newcompany.Industry;

            rtbWebsite.Text = newcompany.Fund;
            tbPhone.Text = newcompany.OfficePhone;
            tbCellphone.Text = newcompany.CellPhone;
            tbFax.Text = newcompany.Fax;
            imgPhoto.ImageUrl = newcompany.Photo;
            imgLogo.ImageUrl = newcompany.Logo;
            rcbCurrency.SelectedValue = newcompany.Monetary;
            tbDescription.Text = newcompany.Description;
            cbCellphone.Checked = newcompany.IsCellPhonePublic;
            cbPhone.Checked = newcompany.IsOfficePhonePublic;
            cbFax.Checked = newcompany.IsFaxPublic;

            rntbFund.Text = newcompany.Fund;

            foreach (ListItem item in rblFirmSize.Items)
            {
                if (item.Text == newcompany.Scale)
                {
                    rblFirmSize.SelectedValue = item.Value;
                    break;
                }
            }

            foreach (RadComboBoxItem item in rcbJobtitle.Items)
            {
                if (item.Text == newcompany.CompanyOwner)
                {
                    rcbJobtitle.SelectedValue = item.Value;
                    break;
                }
            }

            //rblFirmSize.SelectedItem.Text = newcompany.Scale;
            //rcbJobtitle.Text = newcompany.CompanyOwner;

            //取得产品类别
            if (newcompany.Product != string.Empty)
            {

                string[] tmp = newcompany.Product.Split(new string[] { Constant.STR_SPLIT_LISTITEM }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string text in tmp)
                {
                    //rtvCategoryTree.FindNodeByValue(text).Checked = true;
                    rtvCategoryTree.CheckedNodes.Add(rtvCategoryTree.FindNodeByValue(text));
                }

                //foreach (RadTreeNode item in rtvCategoryTree.Nodes)
                //{

                //    foreach (string text in tmp)
                //    {
                //        if (item.Value == text)
                //        {
                //            item.Checked = true;
                //            break;
                //        }
                //    }
                //}

            }

            //取得工艺类别
            if (newcompany.Technology != string.Empty)
            {
                string[] tmp = newcompany.Technology.Split(new string[] { Constant.STR_SPLIT_LISTITEM }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string text in tmp)
                {
                    //rtvProcessTree.FindNodeByValue(text).Checked = true;
                    rtvProcessTree.CheckedNodes.Add(rtvProcessTree.FindNodeByValue(text));

                }

                //foreach (RadTreeNode item in rtvProcessTree.Nodes)
                //{

                //    foreach (string text in tmp)
                //    {
                //        if (item.Value == text)
                //        {
                //            item.Checked = true;
                //            break;
                //        }
                //    }
                //}

            }

        }

        XPCompanyCertInfo cert = (XPCompanyCertInfo)Session[ConstantSession.REG_SUPPLY_COMPYCERT_INFO];
        if (cert == null)
        {
            cert = (XPCompanyCertInfo)Session[ConstantSession.REG_PURCH_COMPYCERT_INFO];
        }
        if (cert != null)
        {
            tbLicense.Text = cert.CertName;

            if (cert.CertFileData != null)
            {
                lnkLicense.Visible = true;
                btnDeleteLicense.Visible = true;
                btnUploadLicense.Visible = false;
                ruploadLicense.Visible = false;

                lnkLicense.Text = cert.CertFileName;
                lnkLicense.NavigateUrl = Encoding.Unicode.GetString(cert.CertFileData);
            }
        }


        //dtCompyPurduct = new DataTable();
        //dtCompyPurduct.Columns.Add("ProductID", typeof(String));
        //dtCompyPurduct.Columns.Add("ProductName", typeof(String));
        //dtCompyPurduct.Columns.Add("ProductDescription", typeof(String));
        //dtCompyPurduct.Columns.Add("ProductPhotoFile", typeof(String));


        //dtCompyCert = new DataTable();
        //dtCompyCert.Columns.Add("CertID", typeof(String));
        //dtCompyCert.Columns.Add("CertName", typeof(String));
        //dtCompyCert.Columns.Add("CertFileName", typeof(String));

        if (Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST] == null)
        {
            Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST] = new List<XPCompanyProductInfo>();
        }

        if (Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST] == null)
        {
            Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST] = new List<XPCompanyCertInfo>();

        }

        rgridCompanyProducts.DataSource = (List<XPCompanyProductInfo>)Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST];
        rgridCompanyProducts.DataBind();

        rgridCompanyCerts.DataSource = (List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST];
        rgridCompanyCerts.DataBind();

        return true;
    }

    private bool saveToSession()
    {

        //Dim newUser As MembershipUser = Membership.GetUser(inp_uname.Text)
        //创建新的企业对象
        XPCompanyInfo newcompany = null;
        XPCompanyCertInfo cert = null;
        if (Session[ConstantSession.REG_SUPPLY_COMPY_INFO] != null)
        {
            newcompany = (XPCompanyInfo)Session[ConstantSession.REG_SUPPLY_COMPY_INFO];
        }
        else
        {
            newcompany = new XPCompanyInfo();
        }

        if (Session[ConstantSession.REG_SUPPLY_COMPYCERT_INFO] != null)
        {
            cert = (XPCompanyCertInfo)Session[ConstantSession.REG_SUPPLY_COMPYCERT_INFO];
        }
        else
        {
            cert = new XPCompanyCertInfo();
        }

        var _with1 = newcompany;
        _with1.VendorModerateStatus = 0;
        //创建企业标识

        _with1.CompanyName = tbCompanyName.Text.Trim();
        _with1.CompanyAliasName = _with1.CompanyName;
        _with1.Industry = rcbIndustry.SelectedItem.Text;


        _with1.WebSite = rtbWebsite.Text.Trim();
        _with1.Fund = rntbFund.Value.ToString();

        _with1.Scale = rblFirmSize.SelectedItem.Text;

        _with1.OfficePhone = tbPhone.Text;
        _with1.CellPhone = tbCellphone.Text;
        _with1.Fax = tbFax.Text.Trim();

        //职位=CompanyOwner?????
        _with1.CompanyOwner = rcbJobtitle.SelectedItem.Text;

        //_with1.Country = rcbCountry.SelectedItem.Text;
        //_with1.Province = rcbProvince.SelectedItem.Text;
        //_with1.City = rcbCity.SelectedItem.Text;
        //_with1.Address = tbAddress.Text.Trim();
        //_with1.PostCode = tbZipcode.Text;


        _with1.Photo = GlobalUtility.ResolveAppRelativeVirtualPath(imgPhoto.ImageUrl);
        _with1.Logo = GlobalUtility.ResolveAppRelativeVirtualPath(imgLogo.ImageUrl);
        _with1.Monetary = rcbCurrency.SelectedValue;
        _with1.Description = tbDescription.Text;

        _with1.IsCellPhonePublic = cbCellphone.Checked;
        _with1.IsOfficePhonePublic = cbPhone.Checked;
        _with1.IsFaxPublic = cbFax.Checked;
        _with1.IsEmailPublic = false;


        ////取得软件类别字符串
        //foreach (RadListBoxItem item in rlbOwnSoftware.Items)
        //{
        //    _with1.Software += item.Text + Constant.STR_SPLIT_LISTITEM;
        //}

        //取得产品类别
        foreach (RadTreeNode node in rtvCategoryTree.CheckedNodes)
        {
            _with1.Product += node.Value + Constant.STR_SPLIT_LISTITEM;
        }
        _with1.Product = _with1.Product.TrimEnd(Constant.STR_SPLIT_LISTITEM.ToCharArray());


        //取得工艺类别
        foreach (RadTreeNode node in rtvProcessTree.CheckedNodes)
        {
            _with1.Technology += node.Value + Constant.STR_SPLIT_LISTITEM;
        }
        _with1.Technology = _with1.Technology.TrimEnd(Constant.STR_SPLIT_LISTITEM.ToCharArray());



        //create license object
        var _with3 = cert;
        //_with3.CompanyID = newcompany.CompanyID;
        //_with3.CreateUserID = newcompany.UserID;

        if (lnkLicense.Text.Trim() != string.Empty)
        //if (ruploadLicense.UploadedFiles.Count > 0)
        {
            //string oldfilename = ruploadLicense.UploadedFiles[0].GetName();

            //string oldpath = Server.MapPath(ruploadLicense.TargetFolder) + "\\" + oldfilename;

            _with3.CertName = tbLicense.Text;
            _with3.CertType = 0;
            _with3.CertFileName = lnkLicense.Text;
            //_with3.CertFileContentType = ruploadLicense.UploadedFiles[0].ContentType;
            //_with3.CertFileSize = ruploadLicense.UploadedFiles[0].ContentLength;

            _with3.CertFileData = Encoding.Unicode.GetBytes(lnkLicense.NavigateUrl);
            //if (System.IO.File.Exists(oldpath))
            //{
            //    _with3.CertFileData = System.IO.File.ReadAllBytes(oldpath);
            //}
        }


        Session[ConstantSession.REG_SUPPLY_COMPY_INFO] = newcompany;

        Session[ConstantSession.REG_SUPPLY_COMPYCERT_INFO] = cert;

        Session[ConstantSession.PRE_PAGE_ID] = Request.FilePath;

        return true;
    }



    protected void rgridCompanyProducts_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        //string productID = ((GridDataItem)e.Item).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProductID"].ToString();

        if (((List<XPCompanyProductInfo>)Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST]).Count < e.Item.ItemIndex)
        {
            rgridCompanyProducts.Controls.Add(new LiteralControl("Unable to delete product. Reason: "));
        }

        ((List<XPCompanyProductInfo>)Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST]).RemoveAt(e.Item.ItemIndex);

    }


    protected void rgridCompanyProducts_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadAsyncUpload upload = (RadAsyncUpload)editMan.GetColumnEditor("ProductPhotoFile").ContainerControl.FindControl("radUploadProduct");

            string oldfilename = upload.UploadedFiles[0].GetName();
            string newfilename = null;
            string oldpath = null;
            string newpath = null;

            oldpath = Server.MapPath(upload.TargetFolder) + "\\" + oldfilename;

            Trace.Warn(oldpath);

            newfilename = FileUtility.CreateUniqueFileName(oldfilename);
            newpath = Server.MapPath(upload.TargetFolder) + "\\" + newfilename;
            System.IO.File.Move(oldpath, newpath);

            XPCompanyProductInfo product = new XPCompanyProductInfo();
            XPCompanyProduct bilXPCP = new XPCompanyProduct();

            var _with5 = product;
            //_with5.CompanyID = new Guid(hfRegedCompany.Value);
            //_with5.CreateUserID = new XPCompany().GetXPCompany(_with5.CompanyID).UserID;
            _with5.ProductPhotoFile = GlobalUtility.ResolveAppRelativeVirtualPath("~/Login/temp" + "/" + newfilename);
            _with5.ProductPhotoTitle = newfilename;
            _with5.ProductName = ((RadTextBox)editMan.GetColumnEditor("ProductName").ContainerControl.FindControl("rtbProductName")).Text;
            _with5.ProductDescription = ((RadTextBox)editMan.GetColumnEditor("ProductDescription").ContainerControl.FindControl("rtbProductDescription")).Text;

            _with5.ProductID = Guid.Empty;
            ((List<XPCompanyProductInfo>)Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST]).Add(_with5);


    }

    protected void rgridCompanyProducts_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        rgridCompanyProducts.DataSource = (List<XPCompanyProductInfo>)Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST];
        //rgridCompanyProducts.DataBind();


        if (e.CommandName == "InitInsert" || e.CommandName == "PerformInsert")
        {
            //if (new XPCompanyProduct().SelectByCompanyID(new Guid(hfRegedCompany.Value)).Tables[0].Rows.Count >= GlobalSetting.MaxCompanyProductsQuantity)
            if (((List<XPCompanyProductInfo>)Session[ConstantSession.REG_SUPPLY_COMPYPRDCT_LIST]).Count >= GlobalSetting.MaxCompanyProductsQuantity)
            {
                rgridCompanyProducts.Controls.Add(new LiteralControl("您的可用产品空间配额已用完."));
                e.Canceled = true;
                return;
            }
        }
    }

    protected void rgridCompanyCerts_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        //string certID = ((GridDataItem)e.Item).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CertID"].ToString();

        List<XPCompanyCertInfo> certLists = (List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST];
        if (((List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST]).Count < e.Item.ItemIndex)
        {
            rgridCompanyCerts.Controls.Add(new LiteralControl("Unable to delete cert. Reason: "));
            return;
        }
        ((List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST]).RemoveAt(e.Item.ItemIndex);


        //XPCompanyCertInfo cert = default(XPCompanyCertInfo);
        //XPCompanyCert bilCorpcert = new XPCompanyCert();
        //try
        //{
        //    cert = bilCorpcert.GetXPCompanyCert(new Guid(certID));
        //    bilCorpcert.Delete(cert);

        //}
        //catch (Exception ex)
        //{
        //    rgridCompanyCerts.Controls.Add(new LiteralControl("Unable to delete cert. Reason: " + ex.Message));
        //    e.Canceled = true;
        //}

    }

    protected void rgridCompanyCerts_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadAsyncUpload upload = (RadAsyncUpload)editMan.GetColumnEditor("CertFileName").ContainerControl.FindControl("radUploadCert");

            string filename = upload.UploadedFiles[0].GetName();
            string filepath = null;
            filepath = Server.MapPath(upload.TargetFolder) + "\\" + filename;

            XPCompanyCertInfo cert = new XPCompanyCertInfo();
            XPCompanyCert bilCorpcert = new XPCompanyCert();
            var _with6 = cert;
            //_with6.CompanyID = new Guid(hfRegedCompany.Value);
            //_with6.CreateUserID = new XPCompany().GetXPCompany(_with6.CompanyID).UserID;

            _with6.CertName = ((GridDropDownColumnEditor)editMan.GetColumnEditor("CertName")).SelectedText;
            _with6.CertType = 1;
            _with6.CertFileName = filename;
            _with6.CertFileContentType = upload.UploadedFiles[0].ContentType;
            _with6.CertFileSize = upload.UploadedFiles[0].ContentLength;

            if (System.IO.File.Exists(filepath))
            {
                _with6.CertFileData = System.IO.File.ReadAllBytes(filepath);
            }


            _with6.CertID = Guid.Empty;
            ((List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST]).Add(_with6);

            rgridCompanyCerts.DataSource = ((List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST]);
            rgridCompanyCerts.DataBind();

            //try
            //{
            //    bilCorpcert.Insert(_with6);
            //    System.IO.File.Delete(filepath);

            //}
            //catch (Exception ex)
            //{
            //    rgridCompanyCerts.Controls.Add(new LiteralControl("Unable to insert into certs. Reason: " + ex.Message));
            //    e.Canceled = true;
            //}

    }

    protected void rgridCompanyCerts_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

        rgridCompanyCerts.DataSource = (List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST];
        //rgridCompanyCerts.DataBind();

        if (e.CommandName == "InitInsert" || e.CommandName == "PerformInsert")
        {
            //if (bilCorpcert.SelectByCompanyID(new Guid(hfRegedCompany.Value)).Tables[0].Rows.Count >= GlobalSetting.MaxCompanyCertsQuantity)
            if (((List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST]).Count >= GlobalSetting.MaxCompanyCertsQuantity)
            {
                rgridCompanyCerts.Controls.Add(new LiteralControl("您上传的认证数目已达最大."));
                e.Canceled = true;
                return;
            }
        }
        else if (e.CommandName == "DownloadCert")
        {
            //string certID = ((GridDataItem)e.Item).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CertID"].ToString();
            XPCompanyCertInfo cert = default(XPCompanyCertInfo);
            try
            {
                cert = ((List<XPCompanyCertInfo>)Session[ConstantSession.REG_SUPPLY_COMPYCERT_LIST])[e.Item.ItemIndex];

                //cert = bilCorpcert.GetXPCompanyCert(new Guid(certID));
                var _with7 = cert;
                string fileName = _with7.CertFileName;
                int fileSize = _with7.CertFileSize;
                Context.Server.ScriptTimeout = 600;
                Context.Response.Buffer = true;
                Context.Response.Clear();
                Context.Response.ContentType = _with7.CertFileContentType;
                Context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
                Context.Response.AddHeader("Content-Length", fileSize.ToString());
                byte[] fileBuffer = new byte[fileSize + 2];
                // buffer of the file data

                Context.Response.BinaryWrite(_with7.CertFileData);

                // End the response
                Context.Response.End();
            }
            catch (Exception ex)
            {
                rgridCompanyCerts.Controls.Add(new LiteralControl("Unable to get cert. Reason: " + ex.Message));
                e.Canceled = true;
            }

        }
    }

    protected void lbtRegFull_Click(object sender, System.EventArgs e)
    {
        if (!RadCaptchaReg.IsValid)
        {
            return;
        }

        if (!hfRegedCompany.Value.Equals(string.Empty))
        {
            MembershipUser reguser = Membership.GetUser(hfRegedCompany.Value);
            if (reguser != null)
            {
                //Dim userid As Guid = New Guid(reguser.ProviderUserKey.ToString)
                //Dim reader As SqlDataReader = XPCompanyUtility.SelectReaderByUserID(userid)

                //If reader.Read Then
                //    Dim company As New XPCompany(reader)
                //    With company

                //        .Photo = GlobalUtility.ResolveAppRelativeVirtualPath(imgPhoto.ImageUrl)
                //        .Logo = GlobalUtility.ResolveAppRelativeVirtualPath(imgLogo.ImageUrl)
                //        .Monetary = rcbCurrency.SelectedValue
                //        .Description = tbDescription.Text
                //    End With




                //Else
                //    'personal user
                //    If Not reader.IsClosed Then
                //        reader.Close()
                //    End If
                //End If


                //rpvRegSuccess.Selected = true;
            }
            else
            {
                //session out of time
                Response.Redirect(GlobalSetting.UrlHomePage);
            }
        }
    }

}